package com.SMP.service;

import com.SMP.dao.PeopleMapper;
import com.SMP.model.People;
import com.SMP.model.PeopleExample;
import com.SMP.utils.Face;
import com.SMP.utils.Utils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.beans.IntrospectionException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.SMP.model.ExcelBean;
import com.SMP.utils.ExcelUtil;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zlz
 * //	 * @param 人员
 */

@Service
public class PeopleService {

    @Autowired
    PeopleMapper peopleMapper;

    /**
     * @Description: 新增人员
     * @Param: [people]
     * @return: int
     * @Author: WYHY
     * @Date: 2018/12/28
     */
    public int addPeople(People people, MultipartFile file) {
        int result =0;
        if (!file.isEmpty()){
            String faceid=Face.addFaceMsg(file);
            if (!faceid.equals("error")&&!faceid.isEmpty()){
                people.setPhoto("/photo/people/"+file.getOriginalFilename());
                people.setStandby5(faceid);
                people.setStandby1("1");
                people.setBirth(people.getStandby2().substring(6, 10) + "-" + people.getStandby2().substring(10, 12) + "-" + people.getStandby2().substring(12, 14));
                people.setInputTime(new Date());
                boolean isIn=true;//Face.inpeople(people,faceid);
                System.out.println("是否录入成功"+isIn);
                if (isIn){
                    result=peopleMapper.insertSelective(people);
                }
            }
        }
        return result;
    }

    /**
     * @Description: 修改人员信息
     * @Param: [people]
     * @return: int
     * @Author: WYHY
     * @Date: 2018/12/28
     */
    public int updPeople(People people) {
        int result = peopleMapper.updateByPrimaryKeySelective(people);
        return result;
    }

    /**
     * @Description: 主键查询人员信息
     * @Param: [id]
     * @return: com.SMP.model.People
     * @Author: WYHY
     * @Date: 2018/12/28
     */
    public People selByPrimaryKey(String id) {
        People people = peopleMapper.selectByPrimaryKey(id);
        return people;
    }

    /**
     * @Description: 条件查询人员信息
     * @Param: [people]
     * @return: java.util.List<com.SMP.model.People>
     * @Author: WYHY
     * @Date: 2018/12/28
     */
    public List<People> selPeoples(People people) {
        PeopleExample example = new PeopleExample();
        example.setDistinct(true);
        PeopleExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(people.getName())&&!StringUtils.isBlank(people.getName())) {
            criteria.andNameLike("%" + people.getName() + "%");
        }
        if (!StringUtils.isEmpty(people.getSex())&&!StringUtils.isBlank(people.getSex())) {
            criteria.andSexEqualTo(people.getSex());
        }
        if (!StringUtils.isEmpty(people.getStandby2())&&!StringUtils.isBlank(people.getStandby2())) {
            criteria.andStandby2Like("%" + people.getStandby2() + "%");
        }
        if (!StringUtils.isEmpty(people.getProfessional())&&!StringUtils.isBlank(people.getProfessional())) {
            criteria.andProfessionalEqualTo(people.getProfessional());
        }
        if (!StringUtils.isEmpty(people.getClasss())&&!StringUtils.isBlank(people.getClasss())) {
            criteria.andClasssEqualTo(people.getClasss());
        }
        if (!StringUtils.isEmpty(people.getGrade())&&!StringUtils.isBlank(people.getGrade())) {
            criteria.andGradeEqualTo(people.getGrade());
        }
        if (!StringUtils.isEmpty(people.getStandby3())&&!StringUtils.isBlank(people.getStandby3())) {
            criteria.andStandby3EqualTo(people.getStandby3());
        }
        if (!StringUtils.isEmpty(people.getStandby5())&&!StringUtils.isBlank(people.getStandby5())) {
            criteria.andStandby5EqualTo(people.getStandby5());
        }
        criteria.andStandby1EqualTo("1");
        List<People> result = peopleMapper.selectByExample(example);
        return result;
    }

    /**
    * @Description: 根据人脸id查询
    * @Param: [faceid]
    * @return: com.SMP.model.People
    * @Author: WYHY
    * @Date: 2019/1/8
    */
    public People selectFace(String faceid){
        return peopleMapper.selectFace(faceid);
    }

//    /**
//     * @Description: 上传附件
//     * @Param: [file, fileType, annexId, cardId ,request]
//     * @Author: WYHY
//     * @Date: 2018/9/7
//     */
//    public String uploadFile(HttpServletRequest request) {
//        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
//        MultipartFile file = null;
//        int num=0;
//        String name="";
//        for (int i=0;i<files.size();i++) {
//            file=files.get(i);
//            String annexId=annexIds.get(i);
//            String cardId=cardIds.get(i);
//            Annex annex = new Annex();
//            if (annexId.equals("N/A")) {
//                annexId = saveAndGetId(annex, cardId);
//                annex.setId(annexId);
//            }
//            if (file != null) {// 判断上传的文件是否为空
//                String type = null;// 文件类型
//                String fileName = file.getOriginalFilename();// 文件原名称
//                type = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toUpperCase() : null;
//                if (type != null && type.equals("PDF") || type.equals("JPG") || type.equals("PNG")) {// 判断文件类型
//                    String filePath = yamlModel.getUpload_path() + cardId + "/" + lastPath[Integer.parseInt(fileType) - 1];
//                    File directory = new File(filePath);
//                    int size = directory.listFiles().length;
//                    String seepath = cardId + "/" + lastPath[Integer.parseInt(fileType) - 1];
//                    String newFileName = cardId + "·" + size + "." + type;
//                    switch (fileType) {
//                        case "1":
//                            annex.setFile1(seepath);
//                            break;
//                        case "2":
//                            annex.setFile2(seepath);
//                            break;
//                        case "3":
//                            annex.setFile3(seepath);
//                            break;
//                        case "4":
//                            annex.setFile4(seepath);
//                            break;
//                        case "5":
//                            annex.setFile5(seepath);
//                            break;
//                        case "6":
//                            annex.setFile6(seepath);
//                            break;
//                        case "7":
//                            annex.setFile7(seepath);
//                            break;
//                        case "8":
//                            annex.setPhoto(seepath);
//                            break;
//                        default:
//                            filePath = "";
//                            break;
//                    }
//                    if (filePath.equals("")) {
//                        name+=fileName+" 文件类型错误！\n";
//                    } else {
//                        baseMapper.updateById(annex);
//                        File writeFile = new File(filePath + newFileName);
//                        try {
//                            //文件写入磁盘
//                            while (writeFile.exists()) {
//                                ++size;
//                                newFileName = cardId + "·" + size + "." + type;
//                                writeFile = new File(filePath + newFileName);
//                            }
//                            file.transferTo(writeFile);
//                            num++;
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                            name+=fileName+" 文件保存失败！\n";
//                        }
//                    }
//                } else {
//                    name+=fileName+" 文件格式错误！\n";
//                }
//            } else {
//                name+="第 "+(i+1)+" 个文件不存在\n";
//            }
//        }
//        if (name.equals("")){
//            return "共上传成功 "+num +" 个文件";
//        }else{
//            return "共上传成功 "+num +" 个文件,失败 "+(files.size()-num)+" 个文件：\n"+name;
//        }
//    }
}
